package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsAftermarket;
import com.yidu.ruifeng_logistics.entity.LogisticsClient;
import com.yidu.ruifeng_logistics.entity.LogisticsOrderVo;
import com.yidu.ruifeng_logistics.service.LogisticsAftermarketService;
import com.yidu.ruifeng_logistics.service.LogisticsOrderService;
import com.yidu.ruifeng_logistics.utils.Tools;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 售后表(LogisticsAftermarket)表控制层
 *
 * @author 李萃
 * @since 2021-03-09 14:24:35
 */
@RestController
@RequestMapping("logisticsAftermarket")
public class LogisticsAftermarketController {
    /**
     *注入售后 服务对象
     */
    @Resource
    private LogisticsAftermarketService logisticsAftermarketService;

    /**
     *注入订单 服务对象
     */
    @Resource
    private LogisticsOrderService logisticsOrderService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return Map<String, Object>
     */
    @RequestMapping("showUpdate")
    public Map<String, Object> showUpdate(Integer id) {
        //使用售后服务对象根据id查询到售后对象
        LogisticsAftermarket logisticsAftermarket = this.logisticsAftermarketService.queryById(id);
        //新建一个hashmap，用来存放返回数据
        Map<String,Object> map = new HashMap<>();
        //将状态置为1
        map.put("state",1);
        //将信息置为找到了用户
        map.put("msg","找到了用户");
        //将查询到的售后对象放入data中
        map.put("data",logisticsAftermarket);
        //将map返回
        return map;
    }

    /**
     * 根据客户id查客户所有订单
     * @param logisticsOrderVo
     * @param page
     * @param limit
     * @param session
     * @return Map<String, Object>
     */
    @ApiOperation(value = "查询订单", notes = "返回订单集合")
    @GetMapping("queryOrderByClientId")
    public Map<String, Object> queryOrderByClientId(LogisticsOrderVo logisticsOrderVo, Integer page, Integer limit, HttpSession session) {
        //将分页参数传入分页助手，调用分页助手的静态方法开始分页
        PageHelper.startPage(page,limit);
        //在session里取到login属性并将其强转为LogisticsClient客户对象
        LogisticsClient client = (LogisticsClient) session.getAttribute("login");
        //将订单vo的客户id设为客户对象的id
        logisticsOrderVo.setClientId(client.getClientId());
        //通过订单服务对象查询订单列表，将订单vo作为参数传入
        List<LogisticsOrderVo> list = logisticsOrderService.queryAll(logisticsOrderVo);

        //查询到集合后，将订单vo集合数据封装到分页助手的PageInfo对象中
        PageInfo<LogisticsOrderVo> pageInfo=new PageInfo<>(list);
        //使用Tools工具类返回layui需要的数据格式，注意long要强转为int
        return Tools.getLayUiMap(list,(int)pageInfo.getTotal());
    }

    /**
     * 新增投诉
     * @param logisticsAftermarket
     * @return Map<String, Object>
     */
    @ApiOperation(value = "新增投诉", notes = "返回包含信息的map")
    @RequestMapping("addAftermarket")
    public Map<String, Object> addAftermarket(LogisticsAftermarket logisticsAftermarket) {
        //使用售后服务对象将传入的售后对象插入，得到一个售后对象
        LogisticsAftermarket insert = logisticsAftermarketService.insert(logisticsAftermarket);
        //新建一个hashmap，用来存放返回数据
        HashMap<String, Object> map = new HashMap<>();
        //判断得到的售后服务对象是否为空
        if(insert!=null){
            //如果不为空将状态置为1，将信息置为操作成功
            map.put("state",1);
            map.put("msg","操作成功！");
        }else {
            //如果为空将状态置为0，将信息置为操作失败
            map.put("state",0);
            map.put("msg","操作失败！");
        }
        //返回map
        return map;
    }

    /**
     * 查询售后
     * @param logisticsAftermarket
     * @param page
     * @param limit
     * @return Map<String, Object>
     */
    @ApiOperation(value = "查询售后", notes = "返回售后集合")
    @GetMapping("queryAll")
    public Map<String, Object> queryAll(LogisticsAftermarket logisticsAftermarket, Integer page, Integer limit) {
        //将分页参数传入分页助手，调用分页助手的静态方法开始分页
        PageHelper.startPage(page,limit);
        //将售后对象作为参数传入售后服务对象的查询所有 方法中，得到售后对象集合
        List<LogisticsAftermarket> logisticsAftermarkets = logisticsAftermarketService.queryAll2(logisticsAftermarket);

        //查询到集合后，将售后对象集合数据封装到分页助手的PageInfo对象中
        PageInfo<LogisticsAftermarket> pageInfo=new PageInfo<>(logisticsAftermarkets);
        //使用Tools工具类返回layui需要的数据格式，注意long要强转为int
        return Tools.getLayUiMap(logisticsAftermarkets,(int)pageInfo.getTotal());
    }

    /**
     * 修改售后数据
     * @param logisticsAftermarket
     * @return Map<String, Object>
     */
    @RequestMapping("update")
    public Map<String, Object> update(LogisticsAftermarket logisticsAftermarket){
        //使用售后服务对象将传入的售后对象进行更新
        logisticsAftermarketService.update(logisticsAftermarket);
        //新建一个hashmap，用来存放返回数据
        Map<String,Object> map = new HashMap<>();
        //将状态置为1
        map.put("state",1);
        //将信息置为保存成功
        map.put("msg","保存成功!");
        //将map返回
        return map;
    }

}
